package com.itheima.web;


import com.alibaba.fastjson.JSON;
import com.itheima.pojo.Brand;
import com.itheima.service.BrandService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 查询所有品牌
 */
@WebServlet("/selectAllServlet")
public class SelectAllServlet extends HttpServlet {
    //定义Service对象，这里好处，只构建1次
    private BrandService brandService = new BrandService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       // 查询所有，所以是doGet 接受 封装 调用 响应
        // 1.调用Service拿到数据
        List<Brand> brandList = brandService.findAll();
        // 2.响应数据给前端，现在我们响应给前端的一定是JSON数据
        String jsonData = JSON.toJSONString(brandList);
        // 3.设置响应头，明确前端，我是JSON数据
        resp.setContentType("text/json; charset=utf-8");
        resp.getWriter().write(jsonData);

    }
}
